package com.cn.hnust.controller;

import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.hnust.pojo.Lfdshop;
import com.cn.hnust.pojo.User;
import com.cn.hnust.service.ILfdshopService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Lfdshop")
public class LfdshopController {

	@Resource
	private ILfdshopService LfdshopService;

	@RequestMapping("/selectLfdshop")
	public void selectLfdshop(HttpServletResponse response, Lfdshop Lfdshop, Integer sid)
			throws IOException {
		List<Lfdshop> list = LfdshopService.selectLfdshop();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/sign")
	public void sign(HttpSession session, HttpServletResponse response, HttpServletRequest request, Lfdshop Lfdshop,
			String address, String brief, String dname, String pwd, Model model, User user, String name, String img,String openid,
			String sex, Integer age, BigDecimal price, String svip, String zaccountnum) throws IOException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 
		System.out.println(df.format(new Date()));//
		
		Lfdshop.setImg("http://localhost:8080/mfyy/Lfdshopimg/" + img);
		Lfdshop.setAddress(address);
		Lfdshop.setBrief(brief);
		Lfdshop.setDname(dname);
		Lfdshop.setName(name);
		Lfdshop.setPrice(price);
		Lfdshop.setPwd(pwd);
		Lfdshop.setStatus(0);
		Lfdshop.setOpenid(openid);
		LfdshopService.insertSelective(Lfdshop);
		Gson gson = new Gson();
		String json = gson.toJson("");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectLfdshopByName")
	public void selectLfdshopByName(HttpServletResponse response, Lfdshop Lfdshop, Integer sid,String name)
			throws IOException {
		List<Lfdshop> list = LfdshopService.selectLfdshopByName(name);
		System.out.println("name:"+name+" list:"+list.size());
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/selectLfdshopByOpenid")
	public void selectLfdshopByOpenid(HttpServletResponse response, Lfdshop Lfdshop, Integer sid,String openid)
			throws IOException {
		Lfdshop = LfdshopService.selectLfdshopByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(Lfdshop);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/selectLfdshopById")
	public void selectLfdshopById(HttpServletResponse response, Lfdshop Lfdshop, Integer id)
			throws IOException {
		Lfdshop = LfdshopService.selectByPrimaryKey(id);
		Gson gson = new Gson();
		String json = gson.toJson(Lfdshop);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
}
